package com.yyfax.ph.main;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

public class MainModel extends Model {
	private static final String TAG = MainModel.class.getSimpleName();

	protected static final int MSG_GET_RSA = 1;
	protected static final int MSG_GET_NOTICE = 2;
	protected static final int APP_CONFIG = 3;

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		BaseResultEntity result = cast(obj);
		switch (result.requestCode) {
			case MSG_GET_RSA:
				RSAResult rsa = cast(obj);
				EventAgent.post(rsa);
				break;
			case MSG_GET_NOTICE:
				NoticeResult notify = cast(obj);
				EventAgent.post(notify);
				break;
			case APP_CONFIG:
				ConfigResult config = cast(obj);
				EventAgent.post(config);
				break;
		}
	}

	@Override
	public Observable getObservable(Object obj, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		switch (functionalCode) {
			case MSG_GET_RSA:
				return service.getRsa(token, new EmptyEntity());
			case MSG_GET_NOTICE:
				return service.getNotice(token, new EmptyEntity());
			case APP_CONFIG:
				return service.getConfig(new ConfigParams());
		}
		return null;
	}

	private interface Service {
		@POST("sys/key/update")
		Observable<RSAResult> getRsa(@Header("token") String token, @Body EmptyEntity rsa);

		@POST("sys/notice/get")
		Observable<NoticeResult> getNotice(@Header("token") String token, @Body EmptyEntity notice);

		@POST("sys/config/get")
		Observable<ConfigResult> getConfig(@Body ConfigParams params);
	}

	static class RSAResult extends BaseResultEntity<String> {
	}

	static class NoticeResult extends BaseResultEntity<NoticeBean> {
	}

	static class ConfigResult extends BaseResultEntity<Config> {
	}

	private static class ConfigParams {
		String type = "compression";
	}

	static class Config {
		Compression compression;
	}

	static class Compression {
		/***
		 * 图片压缩参数，ios(0-1),android(0-100)
		 */
		String quality;
		int maxSize;
		/***
		 * 压缩宽
		 */
		int width;
		/***
		 * 压缩高
		 */
		int height;
		/***
		 * 是否压缩尺寸 true:压缩 false:不压缩
		 */
		boolean resize;
		/***
		 * 图片压缩最小大小，单位K，图片大小超过minSize图片才压缩
		 */
		int minSize;
	}

	public static class NoticeBean {
		public String id;
		public String title;
		public String content;
	}
}
